﻿Imports WMPLib
Imports System.IO
Imports WindowerFFXI

Public Class FormMain
    Dim wmpArtist As String
    Dim wmpTitle As String
    Dim WindowerInit As New WindowerFFXI.Initialize
    Dim WindowerSend As New WindowerFFXI.SendString
    Dim WindowerGetCmd As New WindowerFFXI.GetCmd
    Dim WindowerDisplay As New WindowerFFXI.Display
    Dim displayupdate As Boolean = False
    Public lTextHelper As Integer
    Public lKeyboardHelper As Integer
    Dim poldet As Boolean

    
#Region "Load and Close"
    Private Sub FormMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        '' Auto suze the form
        Me.Size = New Size(157, 55)
        wmp.settings.volume = 15
        If DetectPOL() = True Then
            poldet = True
            WindowerInit.CreateHandlers()
        End If
        Me.TopMost = My.Settings.topmost
        pctNext.Enabled = False
        pctBack.Enabled = False
        pctStop.Enabled = False
        pctPause.Enabled = False
        pctPlay.Enabled = False

    End Sub

    Private Sub pctExit_Click(sender As System.Object, e As System.EventArgs) Handles pctExit.Click
        '' Close the program
        If POLDetect = True Then
            WindowerDisplay.DeleteTextObj("eCounta")
            WindowerDisplay.Flush()
            WindowerInit.DeleteHandlers()
        End If
        End
    End Sub
#End Region

#Region "Player Controls"
    Private Sub pctBack_Click(sender As System.Object, e As System.EventArgs) Handles pctBack.Click
        wmp.Ctlcontrols.previous()
    End Sub

    Private Sub pctStop_Click(sender As System.Object, e As System.EventArgs) Handles pctStop.Click
        wmp.Ctlcontrols.stop()
        lblTrackInfo.Text = "media player stopped"
        pctPlay.Image = My.Resources.blue_hilite04
    End Sub

    Private Sub pctPause_Click(sender As System.Object, e As System.EventArgs) Handles pctPause.Click
        wmp.Ctlcontrols.pause()
    End Sub

    Private Sub pctPlay_Click(sender As System.Object, e As System.EventArgs) Handles pctPlay.Click
        wmp.Ctlcontrols.play()
    End Sub

    Private Sub pctNext_Click(sender As System.Object, e As System.EventArgs) Handles pctNext.Click
        wmp.Ctlcontrols.next()
    End Sub

    Private Sub pctFile_Click(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pctFile.Click
        If e.Button = Windows.Forms.MouseButtons.Left Then
            filemenu.Show(CType(sender, Control), e.Location)
        End If
    End Sub
#End Region

#Region "Menu Items"

    Private Sub OpenPlaylistToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OpenPlaylistToolStripMenuItem.Click
        OpenList()
    End Sub

    Private Sub SettingsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SettingsToolStripMenuItem.Click
        FormSettings.Show()
    End Sub

    Private Sub ToolStripComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lstVolume.SelectedIndexChanged
        wmp.settings.volume = lstVolume.Text
        If mnuMute.Checked = True Then mnuMute.Checked = False
    End Sub

    Private Sub mnuMute_Click(sender As System.Object, e As System.EventArgs) Handles mnuMute.Click
        If mnuMute.Checked = False Then
            wmp.settings.mute = True
            mnuMute.Checked = True
        Else
            wmp.settings.mute = False
            mnuMute.Checked = False
        End If
    End Sub
#End Region

#Region "Moving the form API"
    ''Const's for moving the form with SPI method
    Const WM_NCHITTEST As Integer = &H84
    Const HTCLIENT As Integer = &H1
    Const HTCAPTION As Integer = &H2

    ''Subroutine for moving the form
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_NCHITTEST
                MyBase.WndProc(m)
                If m.Result = HTCLIENT Then m.Result = HTCAPTION
            Case Else
                MyBase.WndProc(m)
        End Select
    End Sub
#End Region

#Region "GUI Image Changes"
    ''Image switches for back button
#Region "Back Button"

    Private Sub pctBack_EnabledChanged(sender As Object, e As System.EventArgs) Handles pctBack.EnabledChanged
        If pctBack.Enabled = True Then
            GUIImage("back", "enable")
        Else
            GUIImage("back", "disable")
        End If
    End Sub

    Private Sub pctBack_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pctBack.MouseDown
        GUIImage("back", "mdown")
    End Sub

    Private Sub pctBack_MouseHover(sender As Object, e As System.EventArgs) Handles pctBack.MouseHover
        GUIImage("back", "hover")
    End Sub

    Private Sub pctBack_MouseLeave(sender As Object, e As System.EventArgs) Handles pctBack.MouseLeave
        GUIImage("back", "leave")
    End Sub

    Private Sub pctBack_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pctBack.MouseUp
        GUIImage("back", "mup")
    End Sub
#End Region

    ''Image Switches for Stop button
#Region "Stop Button"
    Private Sub pctStop_EnabledChanged(sender As Object, e As System.EventArgs) Handles pctStop.EnabledChanged
        If pctBack.Enabled = True Then
            GUIImage("stop", "enable")
        Else
            GUIImage("stop", "disable")
        End If
    End Sub

    Private Sub pctStop_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pctStop.MouseDown
        GUIImage("stop", "mdown")
    End Sub

    Private Sub pctStop_MouseHover(sender As Object, e As System.EventArgs) Handles pctStop.MouseHover
        GUIImage("stop", "hover")
    End Sub

    Private Sub pctStop_MouseLeave(sender As Object, e As System.EventArgs) Handles pctStop.MouseLeave
        GUIImage("stop", "leave")
    End Sub

    Private Sub pctStop_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pctStop.MouseUp
        GUIImage("stop", "mup")
    End Sub
#End Region

    ''Image Switches for Pause button
#Region "Pause Button"
    Private Sub pctPause_EnabledChanged(sender As Object, e As System.EventArgs) Handles pctPause.EnabledChanged
        If pctPause.Enabled = True Then
            GUIImage("pause", "enable")
        Else
            GUIImage("pause", "disable")
        End If
    End Sub

    Private Sub pctPause_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pctPause.MouseDown
        GUIImage("pause", "mdown")
    End Sub

    Private Sub pctPause_MouseHover(sender As Object, e As System.EventArgs) Handles pctPause.MouseHover
        GUIImage("pause", "hover")
    End Sub

    Private Sub pctPause_MouseLeave(sender As Object, e As System.EventArgs) Handles pctPause.MouseLeave
        GUIImage("pause", "leave")
    End Sub

    Private Sub pctPause_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pctPause.MouseUp
        GUIImage("pause", "mup")
    End Sub
#End Region

    ''Image Switches for Play button
#Region "Play Button"
    Private Sub pctPlay_EnabledChanged(sender As Object, e As System.EventArgs) Handles pctPlay.EnabledChanged
        If pctPlay.Enabled = True Then
            GUIImage("play", "enable")
        Else
            GUIImage("play", "disable")
        End If
    End Sub

    Private Sub pctPlay_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pctPlay.MouseDown
        GUIImage("play", "mdown")
    End Sub

    Private Sub pctPlay_MouseHover(sender As Object, e As System.EventArgs) Handles pctPlay.MouseHover
        GUIImage("play", "hover")
    End Sub

    Private Sub pctPlay_MouseLeave(sender As Object, e As System.EventArgs) Handles pctPlay.MouseLeave
        GUIImage("play", "leave")
    End Sub

    Private Sub pctPlay_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pctPlay.MouseUp
        GUIImage("play", "mup")
    End Sub
#End Region

    ''Image Switches for Next button
#Region "Next Button"
    Private Sub pctNext_EnabledChanged(sender As Object, e As System.EventArgs) Handles pctNext.EnabledChanged
        If pctNext.Enabled = True Then
            GUIImage("next", "enable")
        Else
            GUIImage("next", "disable")
        End If
    End Sub

    Private Sub pctNext_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pctNext.MouseDown
        GUIImage("next", "mdown")
    End Sub

    Private Sub pctNext_MouseHover(sender As Object, e As System.EventArgs) Handles pctNext.MouseHover
        GUIImage("next", "hover")
    End Sub

    Private Sub pctNext_MouseLeave(sender As Object, e As System.EventArgs) Handles pctNext.MouseLeave
        GUIImage("next", "leave")
    End Sub

    Private Sub pctNext_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pctNext.MouseUp
        GUIImage("next", "mup")
    End Sub
#End Region

    ''Image Switches for Fi;e button
#Region "File Button"
    Private Sub pctFile_EnabledChanged(sender As Object, e As System.EventArgs) Handles pctFile.EnabledChanged
        If pctFile.Enabled = True Then
            GUIImage("file", "enable")
        Else
            GUIImage("file", "disable")
        End If
    End Sub

    Private Sub pctFile_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pctFile.MouseDown
        GUIImage("file", "mdown")
    End Sub

    Private Sub pctFile_MouseHover(sender As Object, e As System.EventArgs) Handles pctFile.MouseHover
        GUIImage("file", "hover")
    End Sub

    Private Sub pctFile_MouseLeave(sender As Object, e As System.EventArgs) Handles pctFile.MouseLeave
        GUIImage("file", "leave")
    End Sub

    Private Sub pctFile_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pctFile.MouseUp
        GUIImage("file", "mup")
    End Sub
#End Region

    ''Image Switches for Power button
#Region "Power Button"
    Private Sub pctExit_EnabledChanged(sender As Object, e As System.EventArgs) Handles pctExit.EnabledChanged
        If pctExit.Enabled = True Then
            GUIImage("power", "enable")
        Else
            GUIImage("power", "disable")
        End If
    End Sub

    Private Sub pctExit_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pctExit.MouseDown
        GUIImage("power", "mdown")
    End Sub

    Private Sub pctExit_MouseHover(sender As Object, e As System.EventArgs) Handles pctExit.MouseHover
        GUIImage("power", "hover")
    End Sub

    Private Sub pctExit_MouseLeave(sender As Object, e As System.EventArgs) Handles pctExit.MouseLeave
        GUIImage("power", "leave")
    End Sub

    Private Sub pctExit_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pctExit.MouseUp
        GUIImage("power", "mup")
    End Sub
#End Region

    ''Gui image changer sub routine
    ''States: enable, disable, hover, leave, mdown, mup, playing
    Private Sub GUIImage(ByRef GuiItem As String, ByVal state As String)
        Select Case GuiItem

            '' Back Button actions to take depending on the variable for
            Case "back"
                If state = "enable" Then pctBack.Image = My.Resources.blue_hilite01
                If state = "disable" Then pctBack.Image = My.Resources.blue_norm01
                If state = "hover" Then
                    If pctBack.Enabled = True Then
                        pctBack.Image = My.Resources.blue_hilite01glow 'Glow
                    Else
                        pctBack.Image = My.Resources.blue_norm01
                    End If
                End If
                If state = "leave" Then
                    If pctBack.Enabled = True Then
                        pctBack.Image = My.Resources.blue_hilite01
                    Else
                        pctBack.Image = My.Resources.blue_norm01
                    End If
                End If

                If state = "mdown" Then pctBack.Image = My.Resources.blue_norm01

                If state = "mup" Then
                    If pctBack.Enabled = True Then
                        pctBack.Image = My.Resources.blue_hilite01
                    Else
                        pctBack.Image = My.Resources.blue_norm01
                    End If
                End If

                '' Stop Button actions to take depending on the variable 
            Case "stop"
                If state = "enable" Then pctStop.Image = My.Resources.blue_hilite02
                If state = "disable" Then pctStop.Image = My.Resources.blue_norm02
                If state = "hover" Then
                    If pctStop.Enabled = True Then
                        pctStop.Image = My.Resources.blue_hilite02glow 'Glow
                    Else
                        pctStop.Image = My.Resources.blue_norm02
                    End If
                End If
                If state = "leave" Then
                    If pctStop.Enabled = True Then
                        pctStop.Image = My.Resources.blue_hilite02
                    Else
                        pctStop.Image = My.Resources.blue_norm02
                    End If
                End If

                If state = "mdown" Then pctStop.Image = My.Resources.blue_norm02

                If state = "mup" Then
                    If pctStop.Enabled = True Then
                        pctStop.Image = My.Resources.blue_hilite02
                    Else
                        pctStop.Image = My.Resources.blue_norm02
                    End If
                End If

                '' Stop Button actions to take depending on the variable 
            Case "pause"
                If state = "enable" Then pctPause.Image = My.Resources.blue_hilite03
                If state = "disable" Then pctPause.Image = My.Resources.blue_norm03
                If state = "hover" Then
                    If pctPause.Enabled = True Then
                        pctPause.Image = My.Resources.blue_hilite03glow 'Glow
                    Else
                        pctPause.Image = My.Resources.blue_norm03
                    End If
                End If
                If state = "leave" Then
                    If pctPause.Enabled = True Then
                        pctPause.Image = My.Resources.blue_hilite03
                    Else
                        pctPause.Image = My.Resources.blue_norm03
                    End If
                End If

                If state = "mdown" Then pctPause.Image = My.Resources.blue_norm03

                If state = "mup" Then
                    If pctPause.Enabled = True Then
                        pctPause.Image = My.Resources.blue_hilite03
                    Else
                        pctPause.Image = My.Resources.blue_norm03
                    End If
                End If

                '' Stop Button actions to take depending on the variable 
            Case "play"
                If state = "enable" Then pctPlay.Image = My.Resources.blue_hilite04
                If state = "disable" Then pctPlay.Image = My.Resources.blue_norm04
                If state = "playing" Then
                    pctPlay.Image = My.Resources.blue_hilite04glow
                Else
                    If state = "hover" Then
                        If pctPlay.Enabled = True Then
                            pctPlay.Image = My.Resources.blue_hilite04glow
                        Else
                            pctPlay.Image = My.Resources.blue_norm04
                        End If
                    End If
                    If state = "leave" Then
                        If pctPlay.Enabled = True Then
                            If wmp.playState = WMPPlayState.wmppsPlaying Then
                                pctPlay.Image = My.Resources.blue_hilite04glow
                            Else
                                pctPlay.Image = My.Resources.blue_hilite04
                            End If
                        Else
                            pctPlay.Image = My.Resources.blue_norm04

                        End If
                    End If

                    If state = "mdown" Then pctPlay.Image = My.Resources.blue_norm04

                    If state = "mup" Then
                        If pctPlay.Enabled = True Then
                            pctPlay.Image = My.Resources.blue_hilite04
                        Else
                            pctPlay.Image = My.Resources.blue_norm04
                        End If
                    End If
                End If


                '' Stop Button actions to take depending on the variable 
            Case "next"
                If state = "enable" Then pctNext.Image = My.Resources.blue_hilite05
                If state = "disable" Then pctNext.Image = My.Resources.blue_norm05
                If state = "hover" Then
                    If pctNext.Enabled = True Then
                        pctNext.Image = My.Resources.blue_hilite05glow 'Glow
                    Else
                        pctNext.Image = My.Resources.blue_norm05
                    End If
                End If
                If state = "leave" Then
                    If pctNext.Enabled = True Then
                        pctNext.Image = My.Resources.blue_hilite05
                    Else
                        pctNext.Image = My.Resources.blue_norm05
                    End If
                End If

                If state = "mdown" Then pctNext.Image = My.Resources.blue_norm05

                If state = "mup" Then
                    If pctNext.Enabled = True Then
                        pctNext.Image = My.Resources.blue_hilite05
                    Else
                        pctNext.Image = My.Resources.blue_norm05
                    End If
                End If

                '' Stop Button actions to take depending on the variable 
            Case "file"
                If state = "enable" Then pctFile.Image = My.Resources.blue_hilite06
                If state = "disable" Then pctFile.Image = My.Resources.blue_norm06
                If state = "hover" Then
                    If pctFile.Enabled = True Then
                        pctFile.Image = My.Resources.blue_hilite06glow 'Glow
                    Else
                        pctFile.Image = My.Resources.blue_norm06
                    End If
                End If
                If state = "leave" Then
                    If pctFile.Enabled = True Then
                        pctFile.Image = My.Resources.blue_hilite06
                    Else
                        pctFile.Image = My.Resources.blue_norm06
                    End If
                End If

                If state = "mdown" Then pctPause.Image = My.Resources.blue_norm06

                If state = "mup" Then
                    If pctFile.Enabled = True Then
                        pctFile.Image = My.Resources.blue_hilite06
                    Else
                        pctFile.Image = My.Resources.blue_norm06
                    End If
                End If

                '' Stop Button actions to take depending on the variable 
            Case "power"
                If state = "enable" Then pctExit.Image = My.Resources.blue_hilite07
                If state = "disable" Then pctExit.Image = My.Resources.blue_norm07
                If state = "hover" Then
                    If pctExit.Enabled = True Then
                        pctExit.Image = My.Resources.blue_hilite07glow 'Glow
                    Else
                        pctExit.Image = My.Resources.blue_norm07
                    End If
                End If
                If state = "leave" Then
                    If pctExit.Enabled = True Then
                        pctExit.Image = My.Resources.blue_hilite07
                    Else
                        pctExit.Image = My.Resources.blue_norm07
                    End If

                End If

                If state = "mdown" Then pctExit.Image = My.Resources.blue_norm07

                If state = "mup" Then
                    If pctExit.Enabled = True Then
                        pctExit.Image = My.Resources.blue_hilite07
                    Else
                        pctExit.Image = My.Resources.blue_norm07
                    End If
                End If
        End Select
    End Sub
#End Region

#Region "WMP Stuff"
    Private Sub wmp_PlayStateChange(sender As Object, e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles wmp.PlayStateChange
        If wmp.playState = WMPPlayState.wmppsPlaying Then
            pctNext.Enabled = True
            pctBack.Enabled = True
            pctStop.Enabled = True
            pctPause.Enabled = True
            pctPlay.Image = My.Resources.blue_hilite04glow
            UpdateDisplay()
        End If
        If wmp.playState = WMPPlayState.wmppsStopped Then
            DisplayTrack("FFXI Media Player Stopped")
        End If
    End Sub

    Private Sub wmp_CurrentItemChange(sender As Object, e As AxWMPLib._WMPOCXEvents_CurrentItemChangeEvent) Handles wmp.CurrentItemChange
        wmpArtist = wmp.Ctlcontrols.currentItem.getItemInfo("Artist")
        wmpTitle = wmp.Ctlcontrols.currentItem.getItemInfo("Title")
        If wmp.playState = WMPPlayState.wmppsPlaying Or wmp.playState = WMPPlayState.wmppsTransitioning Then
            lblTrackInfo.Text = wmpArtist.Trim + vbNewLine + wmpTitle.Trim
            If My.Settings.Display = "single" Then
                DisplayTrack("FFXI Media Player Now Playing - Artist: " + wmpArtist.Trim + " | Title: " + wmpTitle.Trim)
            ElseIf My.Settings.Display = "multi" Then
                DisplayTrack("FFXI Media Player" + vbNewLine + "Now Playing" + vbNewLine + "Artist: " + wmpArtist.Trim + vbNewLine + "Title: " + wmpTitle.Trim)
            End If
        End If
    End Sub

    Private Sub OpenList()
        Dim playlist As WMPLib.IWMPPlaylist
        Dim newmedia As WMPLib.IWMPMedia

        playlist = wmp.newPlaylist("NowPlaying", "") 'resets the playlist for everytime you open files
        wmp.currentPlaylist = playlist
        OpenFileDialog1.ShowDialog() 'opens dialog box to allow user to select m3u or wpl
        newmedia = wmp.newMedia(OpenFileDialog1.FileName) 'sets the newmedia
        wmp.currentPlaylist.insertItem(0, newmedia) 'inserts the new media into playlist
        pctPlay.Enabled = True
        If My.Settings.Display = "single" Then
            DisplayTrack("FFXI Media Player loaded playlist")
        ElseIf My.Settings.Display = "multi" Then
            DisplayTrack("FFXI Media Player" + vbNewLine + "Loaded playlist")
        End If
        displayupdate = False
    End Sub
#End Region

#Region "WindoweHelper Stuff"
    Public POLDetect As Boolean
    Dim PID() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessesByName("pol")
    Public Function DetectPOL() As Boolean
        If PID.Count = 0 Then
            Return False
        Else
            ' if a pol process is detectrf then it will get the players username
            ' and the ProcessID
            If PID(0).MainWindowTitle.Contains("PlayOnline Viewer") = True Or PID(0).MainWindowTitle = "" Then
                Return False
            Else
                Return True
            End If
        End If
    End Function

    Public Sub DisplayTrack(temp As String)
        If poldet = True Then
            WindowerDisplay.CreateTextObj("eCounta")
            WindowerDisplay.SetTextObj("eCounta", temp, True, True, False)
            WindowerDisplay.BGTextObj("eCounta", True, 0, 0, 0, 160)
            WindowerDisplay.ColorTextObj("eCounta", 255, 255, 255, 255)
            WindowerDisplay.LocTextObj("eCounta", My.Settings.LocX, My.Settings.LocY)
            WindowerDisplay.Flush()
        End If
    End Sub

    Public Sub UpdateDisplay()
        If poldet = True Then
            If displayupdate = True Then
                If My.Settings.Display = "single" Then
                    DisplayTrack("FFXI Media Player Now Playing - Artist: " + wmpArtist.Trim + " | Title: " + wmpTitle.Trim)
                ElseIf My.Settings.Display = "multi" Then
                    DisplayTrack("FFXI Media Player" + vbNewLine + "Now Playing" + vbNewLine + "Artist: " + wmpArtist.Trim + vbNewLine + "Title: " + wmpTitle.Trim)
                End If
            End If
        End If
    End Sub
#End Region

    Public Sub ReloadSettings()
        If poldet = True Then
            wmp.settings.setMode("shuffle", My.Settings.random)
            wmp.settings.setMode("loop", My.Settings.repeat)
        End If
    End Sub


    Private Sub FormMain_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
        Me.Size = New Size(157, 55)
    End Sub
End Class
